#include <gnome.h>
#include <stdio.h>

#include "support.h"

#include "interface.h"
#include "callbacks.h"

#include "preamble.h"

gchar *filename = NULL;
gboolean statusSaved=TRUE;

static void messageboxQuestionSaveCallback(GtkWidget *widget,gint button,gpointer data)
{
  switch(button)
    {
    case 0:			/* button Yes */
      if (filename)
	writeto();
      else
	{
	  gtk_widget_show(create_fileselectionSaveAs());
	  gtk_main();
	}
    case 1:			/* button No */
      *(gint*)data=GNOME_OK;	/* operation of button Yes & No */
      break;
    case 2:			/* button Cancel */
      *(gint*)data=GNOME_CANCEL;
    }

  gtk_main_quit();
}

gint questionSave(void)
{
  GtkWidget *messageboxQuestionSave;
  gint reply;

  if (statusSaved==FALSE)
    {
      messageboxQuestionSave=gnome_message_box_new(_("The source program has been modified.\nDo you want to save it?"),GNOME_MESSAGE_BOX_QUESTION,GNOME_STOCK_BUTTON_YES,GNOME_STOCK_BUTTON_NO,GNOME_STOCK_BUTTON_CANCEL,NULL);
      gtk_signal_connect(GTK_OBJECT(messageboxQuestionSave),"clicked",messageboxQuestionSaveCallback,&reply);
      gtk_widget_show_all(messageboxQuestionSave);
      gtk_main();
      return reply;
    }
  else
    return GNOME_OK;
}

void new(void)
{
  GtkWidget *textSourceProgram;

  display(_("NEW\n"));
  textSourceProgram=lookup_widget(GTK_WIDGET(appMain),"textSourceProgram");
  gtk_text_freeze (GTK_TEXT (textSourceProgram));
  gtk_editable_delete_text (GTK_EDITABLE (textSourceProgram), 0, -1);
  gtk_text_thaw (GTK_TEXT (textSourceProgram));

  g_free(filename);
  filename=NULL;
  statusSaved=TRUE;
  change_title();
  display(PROMPT);
}

void load(void)
{
  GtkWidget *textSourceProgram;
  FILE *fp;
  gchar buffer[1024];
  gint nchars;

  display(_("OPEN %s\n"),filename);

  textSourceProgram=lookup_widget(GTK_WIDGET(appMain),"textSourceProgram");
  if ((fp = fopen (filename, "r")) != NULL)
    {
      gtk_text_freeze (GTK_TEXT (textSourceProgram));
      gtk_editable_delete_text (GTK_EDITABLE (textSourceProgram), 0, -1);
      while ((nchars = fread (buffer, 1, 1024, fp)) > 0)
	{
	  gtk_text_insert (GTK_TEXT (textSourceProgram), NULL, NULL, NULL, buffer,nchars);
	  if (nchars < 1024)
	    break;
	}
      gtk_text_thaw (GTK_TEXT (textSourceProgram));
      fclose (fp);

      statusSaved=TRUE;
      statusAssembled=FALSE;
      change_title();		/* change title of window */
    }
  else
    display(_("cannot open %s!\n"),filename);
  display(PROMPT);
}

void writeto(void)
{
  GtkWidget *textSourceProgram;
  FILE *fp;
  gint nchars;
  gchar * buffer;

  textSourceProgram=lookup_widget(GTK_WIDGET(appMain),"textSourceProgram");

  if ((fp=fopen(filename,"w"))!=NULL)
    {
      buffer=gtk_editable_get_chars(GTK_EDITABLE(textSourceProgram),0,gtk_text_get_length(GTK_TEXT(textSourceProgram)));
      nchars=fwrite(buffer,sizeof(gchar),strlen(buffer),fp);
	  
      fclose(fp);

      if (nchars==strlen(buffer))
	{
	  statusSaved=TRUE;
	  change_title();
	}
      else
	display(_("fail!\n"));
      
      g_free(buffer);
    }
  else
    display(_("fail!\n"));
}
